package com.xxkj.orcfx;

import com.xxkj.orcfx.service.BaiduCoreService;
import com.xxkj.orcfx.web.OcrQuery;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javafx.scene.control.CheckBox;
import java.io.File;

public class Controller {

    private Stage primaryStage;

    public void setPrimaryStage(Stage stage) {
        this.primaryStage = stage;
    }

    private UserSettings userSettings;
    private static final Logger logger = LoggerFactory.getLogger(Controller.class);


    @FXML
    private TextField sourceFileDirText;

    @FXML
    private TextField destFileDirText;

    @FXML
    private ChoiceBox<String> choiceBox;
    @FXML
    private Button submitButton;
    @FXML
    private TextArea logArea;

    @FXML
    private CheckBox showLogsCheckBox;

    @FXML
    private void initialize() {
        userSettings = new UserSettings();
        sourceFileDirText.setText(userSettings.getSourceFileDir());
        destFileDirText.setText(userSettings.getDestFileDir());
        choiceBox.getItems().addAll("解析影像资料", "解析影像资料后查执行人", "移除缓存解析影像资料", "移除缓存解析影像资料后查执行人");
        choiceBox.setValue(userSettings.getSelectedModeOption());
        LogAppender.setLogArea(logArea);
        logArea.setVisible(showLogsCheckBox.isSelected());
    }

    @FXML
    protected void handleTargetChooseFile(ActionEvent event) {
        DirectoryChooser directoryChooser = new DirectoryChooser();
        directoryChooser.setTitle("Open Resource File");
        File file = directoryChooser.showDialog(primaryStage);
        if (file != null) {
            sourceFileDirText.setText(file.getAbsolutePath());
        }

    }

    @FXML
    protected void handleDestChooseFile(ActionEvent event) {
        DirectoryChooser directoryChooser = new DirectoryChooser();
        directoryChooser.setTitle("Open Resource File");
        File file = directoryChooser.showDialog(primaryStage);
        if (file != null) {
            destFileDirText.setText(file.getAbsolutePath());
        }
    }
    @FXML
    private void handleCheckBoxAction() {
        logArea.setVisible(showLogsCheckBox.isSelected());
    }

    @FXML
    private void handleSubmit() {
        logger.info("----开始执行----");
        submitButton.setDisable(true); // Disable button on submit
        // 处理提交按钮的逻辑
        try {
            String sourceDirPath = sourceFileDirText.getText();
            String destDirPath = destFileDirText.getText();
            String selectedOptionTxt = choiceBox.getValue();
            userSettings.saveProperties(sourceDirPath, destDirPath, selectedOptionTxt);
            BaiduCoreService baiduCoreService = new BaiduCoreService();
            OcrQuery ocrQuery = new OcrQuery();
            ocrQuery.setOrgPath(sourceDirPath);
            ocrQuery.setSavePath(destDirPath);
            ocrQuery.setOcrType(getOptionVal(selectedOptionTxt));
            baiduCoreService.imageInfoToExcel(ocrQuery);
        } finally {
            submitButton.setDisable(false);
        }
        logger.info("----执行完成----");
    }


    private String getOptionVal(String txt) {
//解析影像资料", "解析影像资料后查执行人", "移除缓存解析影像资料","移除缓存解析影像资料后查执行人
        if (StringUtils.equals(txt, "解析影像资料")) {
            return "1";
        } else if (StringUtils.equals(txt, "解析影像资料后查执行人")) {
            return "2";
        } else if (StringUtils.equals(txt, "移除缓存解析影像资料")) {
            return "3";
        } else if (StringUtils.equals(txt, "移除缓存解析影像资料后查执行人")) {
            return "4";
        }
        return "0";
    }

}